package com.fiberhome.nas.core.ui.component.image

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateDp
import androidx.compose.animation.core.updateTransition
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material3.Icon
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.fiberhome.nas.core.model.entity.LocalMediaItem
import com.fiberhome.nas.core.model.entity.PhotoInfo

/**
 * 照片组件
 */
@Composable
fun DevicePhotoImage(
    modifier: Modifier = Modifier,
    photo: PhotoInfo,
    editMode: Boolean,
    selected: Boolean,
    photoUrl: String = "",
) {
    Surface(
        modifier = modifier.aspectRatio(1f),
    ) {
        Box {
            val transition = updateTransition(selected, label = "selected")
            val padding by transition.animateDp(label = "padding") { selected ->
                if (selected) 10.dp else 0.dp
            }
            val roundedCornerShape by transition.animateDp(label = "corner") { selected ->
                if (selected) 16.dp else 0.dp
            }
            //图片
            NetWorkImage(
                model = photoUrl,
                modifier = Modifier
                    .matchParentSize()
                    .padding(padding)
                    .clip(RoundedCornerShape(roundedCornerShape))
            )
            //右上角收藏图标
            if (photo.isFavorite) {
                Icon(
                    Icons.Filled.Favorite,
                    tint = Color.Red.copy(alpha = 0.7f),
                    contentDescription = "收藏",
                    modifier = Modifier
                        .align(Alignment.TopEnd)
                        .padding(2.dp)
                )
            }
            //右下角勾选框
            AnimatedVisibility(
                visible = editMode,
                enter = fadeIn(),
                exit = fadeOut(),
                modifier = Modifier.align(Alignment.BottomEnd),
            ) {
                CheckImage(selected = selected)
            }
        }
    }
}

/**
 * 本地照片组件
 */
@Composable
fun LocalPhotoImage(
    modifier: Modifier = Modifier,
    photo: LocalMediaItem,
    editMode: Boolean,
    selected: Boolean,
) {
    Surface(
        modifier = modifier.aspectRatio(1f),
    ) {
        Box {
            val transition = updateTransition(selected, label = "selected")
            val padding by transition.animateDp(label = "padding") { selected ->
                if (selected) 10.dp else 0.dp
            }
            val roundedCornerShape by transition.animateDp(label = "corner") { selected ->
                if (selected) 16.dp else 0.dp
            }
            //图片
            NetWorkImage(
                model = photo.uri,
                modifier = Modifier
                    .matchParentSize()
                    .padding(padding)
                    .clip(RoundedCornerShape(roundedCornerShape))
            )
            //右下角勾选框
            AnimatedVisibility(
                visible = editMode,
                enter = fadeIn(),
                exit = fadeOut(),
                modifier = Modifier.align(Alignment.BottomEnd),
            ) {
                CheckImage(selected = selected)
            }
        }
    }
}